package com.lineage.data.item_etcitem;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FishingPoleRoller extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(FishingPoleRoller.class);
  
  private int _fishpoleid;
  
  public static ItemExecutor get() {
    return new FishingPoleRoller();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int itemobj = data[0];
    L1ItemInstance tgitem = pc.getInventory().getItem(itemobj);
    if (tgitem == null)
      return; 
    useRoller(pc, tgitem, item);
  }
  
  private void useRoller(L1PcInstance pc, L1ItemInstance tgitem, L1ItemInstance item) {
    if (tgitem == null || item == null) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (tgitem.getItem().getUseType() != 42) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (tgitem.getItemId() == 83014 && item.getItemId() != 83004) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (tgitem.getItemId() == 83024 && item.getItemId() != 83023) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (tgitem.getItemId() == 83014 || tgitem.getItemId() == 83024) {
      int maxchargecount = tgitem.getItem().getMaxChargeCount();
      if (tgitem.getChargeCount() + 100 >= maxchargecount) {
        tgitem.setChargeCount(maxchargecount);
      } else {
        tgitem.setChargeCount(tgitem.getChargeCount() + 100);
      } 
      pc.getInventory().updateItem(tgitem, 128);
      pc.getInventory().removeItem(item, 1L);
    } 
    if (tgitem.getItemId() == 83001) {
      L1ItemInstance newitem = ItemTable.get().createItem(this._fishpoleid);
      newitem.setChargeCount(100);
      newitem.setIdentified(true);
      pc.getInventory().storeItem(newitem);
      pc.getInventory().removeItem(tgitem, 1L);
      pc.getInventory().removeItem(item, 1L);
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._fishpoleid = Integer.parseInt(set[1]);
      if (this._fishpoleid <= 0) {
        this._fishpoleid = 83014;
        _log.error("FishingPoleRoller 设置错误:钓竿编号错误! 使用预设83014");
      } 
    } catch (Exception exception) {}
  }
}
